<?php


	class WechatApi
{	
			var $appid;

			var $appsecret;

	public function __construct($appid,$appsecret){

			$this->appid = $appid;

			$this->appsecret = $appsecret;

	}	
	//开发者接口，验证签名

    public function get_user_info(){

		if (!isset($_GET['code']))
	{
			//触发微信返回code码
		
			$url = urlencode('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'].'?id='.$_GET['id']);
           //$oauthurl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=$this->appid&redirect_uri=$url&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect";
   			$oauthurl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=$this->appid&redirect_uri=$url&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
			Header("Location: $oauthurl"); 
		}else
	{
			//获取code码，以获取openid
	    	$code = $_GET['code'];
			$api = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$this->appid&secret=$this->appsecret&code=$code&grant_type=authorization_code";

			$data = json_decode(file_get_contents($api),true);

			$openid = $data['openid'];//获取到的openid
			//return $openid;

			$access_token = $data['access_token'];

		if(isset($openid)){

			$oauth_api = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid&lang=zh_CN";

			$result = json_decode(file_get_contents($oauth_api),true);

			return $result;

			
		}
	}

 }

}
	
	


?>